/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.xml.ws.WebServiceRef;
import model.Chambre;
import model.Client;
import model.Reservation;
import model.TypeChambre;
import ws.Compte;
import ws.WebServiceAuthentificationService;

/**
 *
 * @author Jawad
 */
@Stateful
public class StatefullGestHotelImpl implements RemoteGestHotel {

    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/WebServiceBanque/WebServiceAuthentificationService.wsdl")
    private WebServiceAuthentificationService service;
    @PersistenceContext(unitName = "WebServiceHotelPU")
    EntityManager em;

    @Override
    public List<TypeChambre> getTypes() {
        Query query = em.createQuery("select t from TypeChambre t");
        return query.getResultList();
    }

    @Override
    public boolean dispoChambre(int id_chambre) {
        Query query = em.createQuery("select c from Chambre c where c.id = :x ");
        query.setParameter("x", id_chambre);
        Chambre chambre = (Chambre) query.getResultList().get(0);
        return chambre.isDisp();

    }

    @Override
    public List<Chambre> liste_chambre_by_type(int type_chambre) {
        Query query = em.createQuery("select c from Chambre c where c.typeChambre.id = :x ");
        query.setParameter("x", type_chambre);
        List<Chambre> list = query.getResultList();
        return list;

    }

    @Override
    public Chambre getChambre(int id) {
        Query query = em.createQuery("select c from Chambre c where c.id = :x ");
        query.setParameter("x", id);
        Chambre chambre = (Chambre) query.getResultList().get(0);
        return chambre;
    }

    private Compte getcompte(java.lang.Long numcompte) {
        ws.WebServiceAuthentification port = service.getWebServiceAuthentificationPort();
        return port.getcompte(numcompte);
    }

    @Override
    public Client reserveChambre(Long num_compt, int id_chambre, Date debut, Date fin) {
        Compte aux = this.getcompte(num_compt);
        if (this.getcompte(num_compt).getNumCompte() != 0) {
            Reservation resa = new Reservation(debut, fin, this.getChambre(id_chambre));
            em.persist(resa);
            Client c = new Client(this.getcompte(num_compt).getNom(), this.getcompte(num_compt).getPrenom());
            em.persist(c);
            this.getChambre(id_chambre).setDisp(false);
            return c;
        } else {
            return null;
        }
    }

    @Override
    public List<Reservation> liste_reservation_by_type(int type_chambre) {
        Query query = em.createQuery("select r from Reservation r where r.chambre.typeChambre.id = :x ");
        query.setParameter("x", type_chambre);
        List<Reservation> list = query.getResultList();
        return list;
    }
}
